/*
 * @Author: your name
 * @Date: 2024-06-05 12:50:55
 * @LastEditTime: 2024-06-05 13:17:12
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\56.合并区间.cpp
 */
/*
 * @lc app=leetcode.cn id=56 lang=cpp
 *
 * [56] 合并区间
 */

// @lc code=start
class Solution {
public:
    static bool cmp(vector<int>& a, vector<int>& b){
        if(a[0] == b[0]) return a[1]<b[1];
        return a[0] < b[0];
    }
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        // 先排序
        sort(intervals.begin(), intervals.end(), cmp);
        vector<vector<int>> res;
        int left = intervals[0][0], right = intervals[0][1];
        for(int i = 1; i< intervals.size(); i++){
            if(right >= intervals[i][0]){
                // 重叠了
                right = max(right, intervals[i][1]);
                //cout<<"right:"<<right<<" ";
            }else{
                // 没有重叠
                //cout<<left<<" "<<right<<" ";
                res.push_back({left, right});
                left = intervals[i][0];
                right = intervals[i][1];
                
            }
        }
        res.push_back({left, right});
        return res;
    }
};
// @lc code=end

